package com.example.statistics.common

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import com.example.statistics.R
import com.example.statistics.bean.DocumentBean
import com.example.statistics.ex.fileName
import com.example.statistics.ex.fileSize
import com.example.statistics.ex.fileTypeIcon
import com.example.statistics.ex.sdf
import com.example.statistics.utils.Constants
import com.example.statistics.utils.FilesViewModel
import com.example.statistics.utils.activityFinish
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.glide
import org.jetbrains.anko.sdk27.coroutines.onClick

class FileFragment : BaseCommonListFragment<DocumentBean>() {

    private var model: FilesViewModel? = null
    override val localDataList: List<DocumentBean>
        get() = arguments?.getParcelableArrayList<DocumentBean>(Constants.DATA).orEmpty()
    override val itemLayoutRes: Int
        get() = R.layout.list_item_document

    override val inputSearch: Boolean
        get() = true

    override val inputSearchFunc: (DocumentBean, String) -> Boolean = { bean, str ->
        bean.path?.fileName?.contains(str) ?: false
    }

    fun getSelectorList(): List<DocumentBean> {
        return dataList.filter { it.isChecked }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val factory = ViewModelProvider.NewInstanceFactory()
        model = factory.create(FilesViewModel::class.java)


    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.w("fragment-->", "onattach")
    }

    override fun onResume() {
        super.onResume()
        val fv = FilesViewModel()
       val model =  ViewModelProvider(this,ViewModelProvider.NewInstanceFactory()).get(FilesViewModel::class.java)
        val a = model.getSelector()

        Log.w("fragment-->", "onResume")
        Log.w("fragment-->", a.joinToString())
    }

    override fun onPause() {
        super.onPause()
        Log.w("fragment-->", "onPause")
        val model =  ViewModelProvider(this,ViewModelProvider.NewInstanceFactory()).get(FilesViewModel::class.java)

        model.setSelector(listOf("123"))

    }


    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: DocumentBean, position: Int) {
        val time = "yyyy-MM-dd HH:mm:ss".sdf.format(bean.duration * 1000)
        bean.path?.let {
            injector.text(R.id.tv_file_title, it.fileName)
                    .glide(R.id.iv_file_icon, it.fileTypeIcon)
                    .text(R.id.tv_file_create, "${bean.size.fileSize} | $time")
                    .with<ImageView>(R.id.iv_file_more) { imageView ->
                        imageView.isSelected = bean.isChecked
                        imageView.onClick {
                            bean.isChecked = !bean.isChecked
                            imageView.isSelected = bean.isChecked
                        }
                    }
                    .clicked {
                        mActivity.activityFinish(Constants.DATA to bean)
                    }
        }
    }


}